package beautiful.butterfly.drds.data_exchange.statistics.jvm_info;

import beautiful.butterfly.drds.data_exchange.statistics.JvmInfo;

import java.util.HashMap;
import java.util.Map;


public class MemoryPoolMXBeanNameToMemoryStatusMap
{
    public final Map<String, MemoryStatus> memoryPoolMXBeanNameToMemoryStatusMap = new HashMap<String, MemoryStatus>();

    public String toString()
    {
        StringBuilder sb = new StringBuilder();
        sb.append("\t");
        sb.append(String.format("%-30s | %-30s | %-30s \n", "MEMORY_NAME", "allocation_size", "init_size"));
        for (MemoryStatus memoryStatus : memoryPoolMXBeanNameToMemoryStatusMap.values())
        {
            sb.append("\t");
            sb.append(String.format("%-30s | %-30s | %-30s \n", memoryStatus.name, String.format("%,.2fMB", (float) memoryStatus.maxSize / JvmInfo.mb), String.format("%,.2fMB", (float) memoryStatus.initSize / JvmInfo.mb)));
        }
        return sb.toString();
    }

    public String getDeltaString()
    {
        StringBuilder sb = new StringBuilder();
        sb.append("\n\t [delta memory info] => \n");
        sb.append("\t\t ");
        sb.append(String.format("%-30s | %-30s | %-30s | %-30s | %-30s \n", "NAME", "used_size", "used_percent", "max_used_size", "max_percent"));
        for (MemoryStatus memoryStatus : memoryPoolMXBeanNameToMemoryStatusMap.values())
        {
            sb.append("\t\t ");
            sb.append(String.format("%-30s | %-30s | %-30s | %-30s | %-30s \n",
                    memoryStatus.name, String.format("%,.2f", (float) memoryStatus.usedSize / JvmInfo.mb) + "mb",
                    String.format("%,.2f", (float) memoryStatus.usedPercent) + "%",
                    String.format("%,.2f", (float) memoryStatus.maxUsedSize / JvmInfo.mb) + "mb",
                    String.format("%,.2f", (float) memoryStatus.maxUsedPercent) + "%"));

        }
        return sb.toString();
    }
}
